package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.core.service.base.NuccBaseService;
import cc.rengu.igas.channel.nucc.facade.bean.InstgInfBean;
import cc.rengu.igas.channel.nucc.facade.bean.SignInfoBean;
import cc.rengu.igas.channel.nucc.facade.bean.TrxInfoBean;
import cc.rengu.igas.channel.nucc.facade.request.BusinessAuthSignAcqRequest;
import cc.rengu.igas.channel.nucc.facade.request.BusinessAuthSignRequest;
import cc.rengu.igas.channel.nucc.facade.response.*;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * @Auther wanglei
 * @date 2020/5/9
 * @Desc Created by 王磊 on 2020/5/9
 * epcc.111.001.01 商业委托签约申请报文
 */
public class EpccBusinessAuthSignService extends NuccBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BusinessAuthSignRequest businessAuthSignRequest = new BusinessAuthSignRequest();
        ConvertUtil.convertOutput(businessAuthSignRequest);
        return businessAuthSignRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        BusinessAuthSignRequest businessAuthSignRequest = (BusinessAuthSignRequest) request;
        BusinessAuthSignResponse businessAuthSignResponse = new BusinessAuthSignResponse();
        /*组装网联报文*/
        BusinessAuthSignAcqRequest businessAuthSignAcqRequest = initNuccMsg(businessAuthSignRequest);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        NuccService nuccService = new NuccServiceImpl();
        String trxCtgy = businessAuthSignRequest.getTrxCtgy();
        /*商业委托-网关*/
        if (NuccParamConstant.EPCC_TRX_TYPE_0203.equals(trxCtgy)) {
            VerifySignGwResponse verifySignGwResponse = nuccService.businessGwAuthSign(businessAuthSignAcqRequest);
            if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(verifySignGwResponse.getSysRtnCd())) {
                rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", verifySignGwResponse.getSysRtnCd(), verifySignGwResponse.getSysRtnDesc());
                businessAuthSignResponse.setRespCode(verifySignGwResponse.getSysRtnCd());
                businessAuthSignResponse.setRespCode(verifySignGwResponse.getSysRtnDesc());
            } else {
                if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(verifySignGwResponse.getBizStsCd())) {
                    rglog.error("网联处理失败，bizStsCd=[{}],bizStsDesc=[{}]", verifySignGwResponse.getBizStsCd(), verifySignGwResponse.getBizStsDesc());
                    businessAuthSignResponse.setRespCode(verifySignGwResponse.getBizStsCd());
                    businessAuthSignResponse.setRespDesc(verifySignGwResponse.getBizStsDesc());
                } else {
                    businessAuthSignResponse.setSec(verifySignGwResponse.getSec());
                    businessAuthSignResponse.setRdrctUrl(verifySignGwResponse.getRdrctUrl());
                    businessAuthSignResponse.setDgtlEnvlp(verifySignGwResponse.getDgtlEnvlp());
                    businessAuthSignResponse.setNcrptnSn(verifySignGwResponse.getNcrptnSn());
                }
            }

            /*商业委托-线下*/
        } else if (NuccParamConstant.EPCC_TRX_TYPE_0204.equals(trxCtgy)) {
            CommonResponse commonResponse = nuccService.businessOffAuthSign(businessAuthSignAcqRequest);
            rglog.error("调用网联接口，sysRtnCd=[{}],sysRtnDesc=[{}]", commonResponse.getSysRtnCd(), commonResponse.getSysRtnDesc());
            businessAuthSignResponse.setRespCode(commonResponse.getSysRtnCd());
            businessAuthSignResponse.setRespCode(commonResponse.getSysRtnDesc());

            /*商业委托-快捷*/
        } else {
            BusiAuthSignQuickAcqResponse busiAuthSignQuickAcqResponse = nuccService.businessAuthSignQuick(businessAuthSignAcqRequest);
            if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(busiAuthSignQuickAcqResponse.getSysRtnCd())) {
                rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", busiAuthSignQuickAcqResponse.getSysRtnCd(), busiAuthSignQuickAcqResponse.getSysRtnDesc());
                businessAuthSignResponse.setRespCode(busiAuthSignQuickAcqResponse.getSysRtnCd());
                businessAuthSignResponse.setRespCode(busiAuthSignQuickAcqResponse.getSysRtnDesc());
            } else {
                rglog.error("网联处理结果，bizStsCd=[{}],bizStsDesc=[{}]", busiAuthSignQuickAcqResponse.getBizStsCd(), busiAuthSignQuickAcqResponse.getBizStsDesc());
                businessAuthSignResponse.setRespCode(busiAuthSignQuickAcqResponse.getBizStsCd());
                businessAuthSignResponse.setRespDesc(busiAuthSignQuickAcqResponse.getBizStsDesc());
            }
        }

        chnlBizResponse.setResult(businessAuthSignResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        BusinessAuthSignResponse businessAuthSignResponse = (BusinessAuthSignResponse) response.getResult();
        ConvertUtil.convertInput(businessAuthSignResponse);

    }

    private BusinessAuthSignAcqRequest initNuccMsg(BusinessAuthSignRequest businessAuthSignRequest) throws Exception {
        /*由上游系统控制根据不同交易类别赋值,此处统一转换*/
        BusinessAuthSignAcqRequest businessAuthSignAcqRequest = new BusinessAuthSignAcqRequest();
        /*账户信息*/
        SignInfoBean signInfoBean = new SignInfoBean();
        signInfoBean.setIdNo(businessAuthSignRequest.getIdNo());
        signInfoBean.setMobNo(businessAuthSignRequest.getMobNo());
        signInfoBean.setSgnAcctId(businessAuthSignRequest.getSgnAcctId());
        signInfoBean.setSgnAcctNm(businessAuthSignRequest.getSgnAcctNm());
        signInfoBean.setIdType(businessAuthSignRequest.getIdType());
        signInfoBean.setLglRepIDNo(businessAuthSignRequest.getLglRepIdNo());
        signInfoBean.setLglRepNm(businessAuthSignRequest.getLglRepNm());
        signInfoBean.setSgnAcctTp(businessAuthSignRequest.getSgnAcctTp());
        signInfoBean.setLglRepIDTp(businessAuthSignRequest.getLglRepIdType());
        signInfoBean.setSgnAcctIssrId(businessAuthSignRequest.getSgnAcctIssrId());
        /*受理信息*/
        InstgInfBean instgInfBean = new InstgInfBean();
        instgInfBean.setInstgId(businessAuthSignRequest.getInstgId());
        /*签约信息*/
        TrxInfoBean trxInfoBean = new TrxInfoBean();
        String trxCtgy = businessAuthSignRequest.getTrxCtgy();
        trxInfoBean.setTrxCtgy(trxCtgy);
        if (NuccParamConstant.EPCC_TRX_TYPE_0206.equals(trxCtgy)) {
            trxInfoBean.setTrxId(businessAuthSignRequest.getOriTrxId());
            trxInfoBean.setAuthMsg(businessAuthSignRequest.getAuthMsg());
        } else {
            NuccSeqNoManage NuccSeqNoManage = new NuccSeqNoManageImpl();
            String epccTxnId = NuccSeqNoManage.genEpccSeqNo();
            trxInfoBean.setTrxId(epccTxnId);
        }
        String format = "YYYY-MM-DD'T'hh:mm:ss";
        String dateTime = DateUtil.getCurrentDateTime(format);
        trxInfoBean.setTrxDtTm(dateTime);
        trxInfoBean.setBizTp(businessAuthSignRequest.getBizTp());
        trxInfoBean.setPyeeMrchntNo(businessAuthSignRequest.getPyeeMrchntNo());
        trxInfoBean.setPyeeMrchntNm(businessAuthSignRequest.getPyeeMrchntNm());
        trxInfoBean.setPyeeAreaNo(businessAuthSignRequest.getPyeeAreaNo());
        trxInfoBean.setPyeeCtryNo(businessAuthSignRequest.getPyeeCtryNo());
        trxInfoBean.setPyeeIdNo(businessAuthSignRequest.getPyeeIdNo());
        trxInfoBean.setPyeeIdTp(businessAuthSignRequest.getPyeeIdTp());
        trxInfoBean.setEntrstAmtLmt(businessAuthSignRequest.getEntrstAmtLmt());
        trxInfoBean.setEntrstAuthNo(businessAuthSignRequest.getEntrstAuthNo());
        trxInfoBean.setEntrstDtDsc(businessAuthSignRequest.getEntrstDtDsc());
        trxInfoBean.setEntrstDtStp(businessAuthSignRequest.getEntrstDtStp());
        trxInfoBean.setEntrstDtUnt(businessAuthSignRequest.getEntrstDtUnt());
        trxInfoBean.setEntrstStrDtTm(businessAuthSignRequest.getEntrstStrDtTm());
        trxInfoBean.setEntrstEndDtTm(businessAuthSignRequest.getEntrstEndDtTm());

        businessAuthSignAcqRequest.setSgnInf(signInfoBean);
        businessAuthSignAcqRequest.setInstgInf(instgInfBean);
        businessAuthSignAcqRequest.setTrxInf(trxInfoBean);
        return businessAuthSignAcqRequest;
    }
}
